<template>
  <el-dialog
    v-model="visible"
    title="监测数据路径与参数设置"
    width="600px"
    destroy-on-close
  >
    <el-form :model="form" label-width="150px" label-position="left">
      <el-form-item label="雷达数据文件路径">
        <el-input
          v-model="form.filepath"
          placeholder="请输入雷达数据文件夹的完整路径，例如：C:\雷达数据\2024 或 /home/user/radar_data"
          clearable
        />
        <div class="path-tips">
          💡 请输入完整的绝对路径，如：
          <br>Windows: <code>C:\用户\文档\雷达数据</code>
          <br>Linux/Mac: <code>/home/username/radar_data</code>
        </div>
      </el-form-item>

      <el-form-item label="GNSS数据文件路径">
        <el-input
          v-model="form.gnss_filepath"
          placeholder="请输入GNSS数据文件夹的完整路径，例如：D:\GNSS数据\监测点 或 /var/data/gnss"
          clearable
        />
        <div class="path-tips">
          💡 请输入完整的绝对路径，如：
          <br>Windows: <code>D:\监测数据\GNSS</code>
          <br>Linux/Mac: <code>/usr/local/gnss_data</code>
        </div>
      </el-form-item>

      <el-form-item label="数据刷新间隔 (分钟)">
        <el-input-number v-model="form.interval" :min="1" />
      </el-form-item>

      <el-form-item label="采样率 (点/秒)">
        <el-input-number v-model="form.rate_pt" :min="1" />
      </el-form-item>

      <el-form-item label="精度阈值">
        <el-input-number v-model="form.prec" :step="0.1" :min="0.1" />
      </el-form-item>

      <el-form-item label="形变类型" prop="xbstyle">
        <el-select v-model="form.xbstyle" placeholder="请选择形变类型">
          <el-option label="单次形变" :value="0" />
          <el-option label="累积形变" :value="1" />
        </el-select>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="close">取消</el-button>
      <el-button type="primary" :loading="isSaving" @click="handleSave">
        保存
      </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { ElMessage } from "element-plus";
import axios from "axios";

const visible = ref(false);
const isSaving = ref(false);

const form = ref({
  filepath: "",
  gnss_filepath: "",
  interval: 10,
  rate_pt: 5,
  prec: 0.1,
  xbstyle: 0,
});

/**
 * ✅ 打开时加载上次配置
 */
const open = async () => {
  visible.value = true;
  try {
    const res = await axios.get("/set_filepath/");
    if (res.data) Object.assign(form.value, res.data);
  } catch {
    ElMessage.info("暂无历史配置");
  }
};

/**
 * ✅ 保存配置
 */
const handleSave = async () => {
  // 验证路径格式
  if (form.value.filepath && !isValidPath(form.value.filepath)) {
    ElMessage.warning("雷达数据文件路径格式不正确，请输入完整的绝对路径");
    return;
  }

  if (form.value.gnss_filepath && !isValidPath(form.value.gnss_filepath)) {
    ElMessage.warning("GNSS数据文件路径格式不正确，请输入完整的绝对路径");
    return;
  }

  if (!form.value.filepath && !form.value.gnss_filepath) {
    ElMessage.warning("请至少设置一个数据文件路径");
    return;
  }

  isSaving.value = true;
  try {
    await axios.post("/set_filepath/", form.value);
    ElMessage.success("保存成功");
    close();
  } catch {
    ElMessage.error("保存失败");
  } finally {
    isSaving.value = false;
  }
};

/**
 * ✅ 验证路径格式
 */
const isValidPath = (path: string): boolean => {
  if (!path || path.trim() === '') return false;
  
  // Windows 路径验证 (如: C:\folder\subfolder 或 \\server\share)
  const windowsPathRegex = /^[a-zA-Z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$|^\\\\[^\\/:*?"<>|\r\n]+\\[^\\/:*?"<>|\r\n]+/;
  
  // Linux/Mac 路径验证 (如: /home/user/folder 或 /usr/local)
  const unixPathRegex = /^\/(?:[^\/\0]+\/)*[^\/\0]*$/;
  
  return windowsPathRegex.test(path) || unixPathRegex.test(path);
};

const close = () => {
  visible.value = false;
};

defineExpose({ open, close });
</script>

<style scoped>
.el-input {
  width: 100%;
}

.path-tips {
  font-size: 12px;
  color: #909399;
  margin-top: 8px;
  line-height: 1.4;
  background: #f4f4f5;
  padding: 8px 12px;
  border-radius: 4px;
  border-left: 3px solid #409eff;
}

.path-tips code {
  background: #e8f4ff;
  padding: 2px 6px;
  border-radius: 3px;
  font-family: 'Courier New', monospace;
  color: #409eff;
}
</style>